<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="200px"
    >
      <el-form-item label="Commodity code" prop="commodityCode">
        <el-input
          v-model="queryParams.commodityCode"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Third country duty" prop="thirdCountryDuty">
        <el-input
          v-model="queryParams.thirdCountryDuty"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="CLASSIFICATION" prop="classification">
        <el-input
          v-model="queryParams.classification"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SECTION" prop="section">
        <el-input
          v-model="queryParams.section"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Section title" prop="sectionTitle">
        <el-input
          v-model="queryParams.sectionTitle"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Chapters" prop="chapters">
        <el-input
          v-model="queryParams.chapters"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    </el-form>
    <div class="textcenter">
      <el-button
        type="primary"
        icon="el-icon-search"
        size="mini"
        v-hasPermi="['source:customs:query']"
        @click="handleQuery"
        >搜索</el-button
      >
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置</el-button
      >
      <el-button
        icon="el-icon-upload2"
        size="mini"
        :disabled="upload.isUploading"
        @click="handleImport"
        v-hasPermi="['source:customs:import']"
        >导入</el-button
      >
    </div>
    <el-table v-loading="loading" :data="CodesList" class="mt-10">
      <el-table-column
        label="Commodity code"
        align="center"
        prop="commodityCode"
      />
      <el-table-column
        label="Third country duty"
        align="center"
        prop="thirdCountryDuty"
      />
      <el-table-column
        label="CLASSIFICATION"
        align="center"
        prop="classification"
      />
      <el-table-column label="SECTION" align="center" prop="section" />
      <el-table-column
        label="Section title"
        align="center"
        prop="sectionTitle"
      />
      <el-table-column label="Chapters" align="center" prop="chapters" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['source:customs:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 导入 -->
    <el-dialog
      title="导入"
      :visible.sync="openImport"
      width="500px"
      append-to-body
    >
      <el-form
        :model="formDr"
        :rules="rulesDr"
        ref="formDr"
        label-width="120px"
      >
        <el-form-item label="选择导入文件：" required>
          <el-upload
            ref="upload"
            class="upload-demo"
            :limit="1"
            accept=".xlsx, .xls"
            :headers="upload.headers"
            :action="upload.url + '?type=' + upload.type"
            :disabled="upload.isUploading"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :auto-upload="false"
          >
            <el-button type="primary" size="small">选择文件</el-button>
            <span slot="tip" class="el-upload__tip"> 支持上传xls/xlsx文件</span>
          </el-upload>
        </el-form-item>
      </el-form>
      <el-link type="primary" class="download-template" @click="downloadExcel"
        >点此下载导入模板</el-link
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </span>
    </el-dialog>

    <!-- 导入结果 -->
    <el-dialog
      title="导入完成"
      :visible.sync="openImportWc"
      width="500px"
      append-to-body
    >
      <div>
        <i class="el-icon-warning-outline fon" v-if="error" />
        <i class="el-icon-circle-check fon success" v-else />
        <div class="downlmplate download-template">
          本次导入成功 {{ completed.successNum }} 条，失败
          {{ completed.errorNum }} 条。
          <el-link type="primary" class="mt10" @click="downloadExcel" v-if="completed.errorNum != 0">
            点此下载失败记录数据</el-link
          >
        </div>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="openImportWc = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listCodesAll, delCodesAll } from "@/api/system/customs";
import { getToken } from "@/utils/auth";
export default {
  name: "customs",
  data() {
    return {
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        type: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/codes/upload",
      },
      openImport: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 明细数据表格数据
      CodesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        commodityCode: null,
        thirdCountryDuty: null,
        classification: null,
        section: null,
        sectionTitle: null,
        chapters: null,
        commodity: null,
        hsCode: null,
        thirdCountryDuty: null,
        CodesDegree: null,
        matchPattern: null,
        type: 0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      formDr: {
        file: null,
      },
      rulesDr: {
        file: [
          { required: true, message: "请选择导入文件", trigger: "change" },
        ],
      },
      fileList: [],
      openImportWc: false,
      error: true,
      completed: {
        successNum: 10,
        errorNum: 1,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleRemove(file, fileList) {
      this.formDr.file = this.fileList = fileList.filter((i) => {
        return i.uid != file.uid;
      });
    },
    handleFileChange(file) {
      this.$refs.formDr.clearValidate();
      this.formDr.file = file;
      this.fileList = [file];
      console.log(this.formDr.file);
    },
    handleImport() {
      this.openImport = true;
      // this.$refs.upload.clearFiles();
    },
    // 导入取 消
    handleCancel() {
      this.openImport = false;
      this.$refs.formDr.resetFields();
      this.fileList = [];
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.openImport = false;
      this.$refs.upload.clearFiles();
      this.getList();
      // this.openImportWc = true;
      // this.loading = false
      // if (response.errorNum != 0) {
      //   this.error = true;
      // }
      // this.completed.successNum = response.successNum;
      // this.completed.errorNum = response.errorNum;
      this.$modal.msgSuccess(response.success);
    },
    // 导入确 定
    handleConfirm() {
      this.$refs.formDr.validate((valid) => {
        if (valid) {
          this.error = false;
          this.$refs.upload.submit();
          this.openImport = false
          // this.loading = true
        }
      });
    },

    // 下载模板
    downloadExcel() {
      this.download(
        "/system/codes/downloadFile?type=0",
        {},
        `文件模板_${new Date().getTime()}.xlsx`
      );
    },
    /** 查询明细数据列表 */
    getList() {
      this.loading = true;
      listCodesAll(this.queryParams).then((response) => {
        this.CodesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除?")
        .then(function () {
          return delCodesAll(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped>
.download-template {
  margin-left: 120px;
  margin-top: -10px;
  margin-bottom: 20px;
}
.fon {
  font-size: 40px;
  margin-left: 20px;
}
.downlmplate {
  margin-top: -30px;
}
.mt10 {
  margin-top: 10px;
}
.textcenter {
  text-align: center;
}
</style>
